<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="isquirrel.beans.User" %>
<%@page import="isquirrel.UserManager" %>
<%@page import="isquirrel.beans.Friend" %>
<%@page import="isquirrel.beans.Url" %>
<%@page import="isquirrel.util.Utilities" %>
<%@page import="isquirrel.FacebookAPI" %>
<%@page import="java.util.Vector" %>
<%@page import="java.util.Set" %>
<%@page import="java.net.URLEncoder" %>
<%@page import="java.net.URLDecoder" %>


<%
	HibernateUtil.beginTransaction();
	User crnt_user = null;
	if (Utilities.isVisitorLoggedIn(session)) {
		crnt_user  = UserManager.getUser((String)session.getAttribute("user"));
	}
	HibernateUtil.commit();
	
	FacebookAPI api = new FacebookAPI();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="isquirrel.util.HibernateUtil"%><html>
<head>
	<link REL="shortcut icon" href="/<%= request.getRequestURI().split("/")[1] %>/images/favicon.ico">
    <link href="/<%= request.getRequestURI().split("/")[1] %>/css/isquirrel.css" media="screen" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="/<%= request.getRequestURI().split("/")[1] %>/js/prototype.js"></script>
    <script type="text/javascript" src="/<%= request.getRequestURI().split("/")[1] %>/js/scriptaculous/scriptaculous.js"></script>
    <script type="text/javascript" src="/<%= request.getRequestURI().split("/")[1] %>/js/fudel.js"></script>
	<script type="text/javascript" src="/<%= request.getRequestURI().split("/")[1] %>/js/isquirrel.js"></script>
	<script type="text/javascript" src="/<%= request.getRequestURI().split("/")[1] %>/js/isquirreltabs.js"></script>
	<script type="text/javascript" src="/<%= request.getRequestURI().split("/")[1] %>/js/swfobject.js"></script>
	<script type="text/javascript" src="/<%= request.getRequestURI().split("/")[1] %>/js/isquirrelplayer.js"></script>
	<title><%=crnt_user.getUsername() %> on iSquirrel</title>
	<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body onLoad="initPage()">
	<img style="position:relative;z-index:600;float:right;" src="/<%= request.getRequestURI().split("/")[1] %>/images/isquirrel.png" />
	<div id="wrapper">
		<div id="header">
			<div id="majorhead">
			</div>
		</div>
		<div id="navmenu">
				<ul>
					<li style="color:black" ><a class='navz' href="/<%= request.getRequestURI().split("/")[1] %>/home">Recommendations</a></li>
					<li style="color:black" ><a class='navz' href="/<%= request.getRequestURI().split("/")[1] %>/network">Your network</a></li>
					<li style="color:black" ><a class='navz' href="/<%= request.getRequestURI().split("/")[1] %>/profile">Profile</a></li>
					<li style="color:black" ><a class='navz' href="/<%= request.getRequestURI().split("/")[1] %>/servlet/LogoutUser">Log out</a></li>
				</ul>
		</div>
		<div id="pagehead">
			
			<div id="usercontainer">
				<div id="piccontainer">
				</div>
				<div id="usr_name">
				</div>
			</div>
			<div id="pagetitle">
		
			</div>
			<div id="pagetabs">
		
			</div>
		</div>	
		<div id="maincontainer">
			<div id="leftcontainer">
				<div id="likes">
					<div style="padding:5px 0px 5px 10px;font-size:1.6em;">
						Likes
					</div>
					<div style="padding:0px 0px 5px 10px;font-size:0.60em">
						<%
						if (crnt_user.getIsProfileInit() == 1) {
							out.println("Liked " + (Integer) session.getAttribute("urlsnum") + " pages.");
						}
					%>
					</div>
				</div>
				<div class="listcontainer" id="likeslist">
					<div class="frontlistheader">
						<span style="position:relative;left:0px" >Pages you've liked</span><span><b><a class="viewall" href="/<%= request.getRequestURI().split("/")[1]%>/servlet/Profiler?nextp=profile&force=<%= URLEncoder.encode("pagesyou'veliked", "UTF-8") %>">View All</a></b></span>
					</div>
					<div id="isqlikesthumbs">
					<div style="position:relative;display:block;height:2px;width:100%"></div>
					<% 
					if (crnt_user.getIsProfileInit() == 1) {
						
						if ((Integer)session.getAttribute("urlsnum")==0){
							/* The user has no friends */
							out.print("<div class='nolist'> You haven't liked any pages yet.</div>");
						}
						else{
							Vector<Url> urls = (Vector<Url>) session.getAttribute("urls");
							for (Url u : urls){
								if (u != null){
									String title = u.getTitle();
									title = URLDecoder.decode(title, "UTF-8");
									String tooltip = title;
									if (title.length() > 20){
										title = title.substring(0,21).trim() + "...";
										//System.out.println(title);
									}
									out.print("<div title='" + tooltip + "' class='likecontainer' style='background-image:url(/iSquirrel/images/" + u.getThumb() + ")'><a class='like' target='_blank' href='" + u.getAddress() +"'>" + title + "</a></div>");
								}
							}
						}
					}
					
					%>
					</div>
				</div>
				<div id="isqfriends">
					<div style="padding:5px 0px 5px 10px;font-size:1.6em;">
						Friends
					</div>
					
					<div style="padding:0px 0px 5px 10px;font-size:0.60em">
					<%
					if (crnt_user.getIsProfileInit() == 1) {
						out.println((Integer) session.getAttribute("friendsnum") + " friends on iSquirrel.");
					}
					%>
					</div>
				</div>
				<div class="listcontainer" id="isqfriendslist">
					<div class="frontlistheader">
						<span style="position:relative;left:0px" >Your friends</span><span class="viewall"><b><a class="viewall" href="/<%= request.getRequestURI().split("/")[1]%>/servlet/Profiler?nextp=network&force=<%= URLEncoder.encode("yourfriends","UTF-8") %>">View All</a></b></span>					</div>
					<div id="isqfriendsthumbs">
					<%
						if (crnt_user.getIsProfileInit() == 1) {
							
							if ((Integer)session.getAttribute("friendsnum")==0){
								/* The user has no friends */
								out.print("<div class='nolist'> You have no friends yet.</div>");
							}
							else{
								Vector<Friend> friends = (Vector<Friend>) session.getAttribute("friends");
								friends.setSize(5);
								HibernateUtil.beginTransaction();
								for (Friend f : friends){
									if (f != null) {
										
										String pic = f.getPic();
										if (pic.equals("nopicmale.png") || pic.equals("nopicfemale.png")) {
											pic = "/" + request.getRequestURI().split("/")[1] +"/images/" + pic;
										}
									out.print("<div style='height:56px' class='friendcontainer'>" +
											     "<img class='friendimg' src='"  + pic + "' />" +
											     "<div class='frienddetails'>" + 
											        "<div class='friendname'><b>" +
											   		   f.getName() +
											        "</b></div>" +
											        "<div style='margin-bottom:2px'>" +
											           "Liked " + f.getLikes() + " pages." + 
											        "</div>" +
											        "<div style='margin-bottom:1px'>" +
														"Profile match: " + f.getProfileMatch_formatted() +											        
											        "</div>" +
											     "</div>" +
											  "</div>");
									}
								}
								HibernateUtil.commit();
							}
						}
					%>
					</div>
					<!-- div class="listalllink">
					View more..</div>
				</div-->
			</div>
			</div>	
			<script type="text/javascript">
				var usr_name = '<%= crnt_user.getFirstName()%><br><%= crnt_user.getLastName() %>'
				<%
				String pic = "/" + request.getRequestURI().split("/")[1] + "/images/";
				if (crnt_user.getFbID() == 0){ 
					pic += (crnt_user.getSex().equals("Male")) ? "nopicmale.png" : "nopicfemale.png";
				 }
				else{
					pic = api.getUser_PicSquare(String.valueOf(crnt_user.getFbID()));
				}
				if (pic == null){
					// rarely the fb call returns null so to avoid any exceptions from setting img.src = null
					// we set the image to a 'no image' pic.
					pic = "/" + request.getRequestURI().split("/")[1] + "/images/";
					pic += (crnt_user.getSex().equals("Male")) ? "nopicmale.png" : "nopicfemale.png";
				}
				%>
				var img = new Element("img");
				img.src= '<%= pic %>';
				$("piccontainer").insert({bottom:img});	
			
			</script>
			<% if (crnt_user.getIsProfileInit() == 0) { %>
	
				<div style="text-align:center" id="basecontainer">
					<p style="font-size:1.6em; color: #bbb">Welcome to iSquirrel. Please hold on while we initialize your profile.</p>
					<div>
						<img src="/<%= request.getRequestURI().split("/")[1] %>/images/profilespinner.gif" />
					</div>
				</div>
				<script src="http://static.ak.facebook.com/js/api_lib/v0.4/XdCommReceiver.js" type="text/javascript"></script>
				<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
				<script type="text/javascript">
					$("pagetitle").update("Initializing your profile");

					FB_RequireFeatures(["Api"], function(){ 

						var api_key = '748303a5c47afd1a0823809e012c155e'; 
						FB.Facebook.init(api_key, "/<%= request.getRequestURI().split("/")[1] %>/xd_receiver.htm")
						var api = FB.Facebook.apiClient;
				
						FB.Connect.requireSession(function(exception){

							api.friends_get(new Array(), function(result, exception){
								var friends = result.join("+");
								
								new Ajax.Request('/<%= request.getRequestURI().split("/")[1]%>/' + 'servlet/Profiler',{
								
										method: "post",
										parameters: {type:1, friends: friends},
										onSuccess: function(response){

											Fudel.redirect('/<%= request.getRequestURI().split("/")[1] %>','servlet/Profiler?nextp=home');
												
										}
							});	 

							}); 

							
							
						});
					});
					
				</script>
		
				<% } else {%>	
					
					<img id="spinner" style="position:absolute;left:63%;display:none;top:50px" src="/<%= request.getRequestURI().split("/")[1] %>/images/tabload.gif" />
					<div id="basecontainer">
		
					</div>
			
					<!-- included content -->
					<div id="content">	
	 					<%
	 						String url;
	 						if (request.getAttribute("p") != null)
								url = "../views/" + request.getAttribute("p");
	 						else
	 							url = "../views/" + request.getParameter("p");
						 %>	
					<jsp:include page='<%= url %>' />
					</div>
					<% } %>
			</div>
	</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-12345417-1");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>